$_SERVER['PHP_SELF']?action="<?php echo htmlspecialchars($_SERVER["PHP_SELF"]); ?>"?htmlspecialchars() do?htmlspecialchars()?
...
<?php
...
if (empty($_POST["courses"])) {
$coursesErr = "Courses are required";
} else {
$aCourses = $_POST['courses'];
$courses = '';
for ($i = 0; $i < count($aCourses); $i++) // or count($_POST['courses'])
$courses .= $aCourses[$i] . ' '; // or $_POST['courses'][$i++]
}
...
?>
...
<form ...>
Courses:
<input type="checkbox" name="courses[]" value="COMP2680">COMP2680
<input type="checkbox" name="courses[]" value="COMP3540">COMP3540
<input type="checkbox" name="courses[]" value="COMP4620">COMP4620
<span class="error">* <?php echo $coursesErr;?></span>
<br>
Cars:
<select name="cars[]" multiple>
<option value="volvo">Volvo</option>
<option value="saab">Saab</option>
<option value="opel">Opel</option>
<option value="audi">Audi</option>
</select>
</form>
...
$.post(..., { 'courses[]': ['volvo', 'open']}, function(data) {...});
...
<input type='hidden' name:'first' value:'true'>
<input type='hidden' name:'first' value:'false'>
preg_match()?filter_var()?preg_match_all().preg_match_all(). How do you interpret array &$matches?filter_var() for FILTER_VALIDATE_EMAIL and FILTER_VALIDATE_URL.(?=PATTERN_HERE) should be in the next following pattern.(?!PATTERN_HERE) should not be in the next following pattern.
$password_pattern = '/^(?=.*[!@#$%^&*()\-_+=])(?=.*[0-9]) ...$/' Be careful with .* in the assertions.
if (preg_match($password_pattern, $password))
...
else
...
(?=.*[!@#$%^&*()\-_+=]) means there should be [!@#$%^&*()\-_+=] (i.e., a special character)
after .* (i.e., any characters).(?=.*[0-9]) means there should be [0-9] (i.e., a digit) after .* (i.e., any characters).
// An example of a regular expression for passwords
let p = "Abc!123";
let regExpPassword = /^(?=.*[0-9]).{5,}$/; // Regular expressions are objects in JS.
let result = regExpPassword.test(p);
alert(result);
// Another example to extract patterns
let d = new Date();
let n = d.toTimeString(); // n includes the timezone. E.g., 11:22:54 GMT-0800 (Pacific Standard Time)
alert(n);
let regExp = /\(.*\)/; // It is NOT a string. Is it a sort of object?
let matches = n.match(regExp);
alert(matches[0].substr(1, matches[0].length - 2)); // matches[0] includes this string "(...)".
.test() and .match())
<input type='submit' ...>, the form will submit inputs to the action URL.
You'd better use <input type='button' ...> instead,
so that even when the user clicks the button, the form does not submit inputs automatically.
<script>
???.???('test_form_button').???('click', function() {
????
???.???('???').submit(); // submit() method in the form object
});
</script>
<form id='test_form' action='controller.php'>
Username: <input id='username' type='text' name='username'><br>
Password: <input id='password' type='???' name='password'><br>
<input id='test_form_button' type='???' value='Submit'> // Just button, not submit
// If submit button is used, the click event may not be captured.
</form>
// Example of username
???.???('test_form_button').???('click', function() {
if (!(/^[_a-z][_a-z0-9]{3,}$/i.???(???.???('username').value))) {
...
}
else
???.???('???').submit(); // submit() method in the form object
// or
let pattern = /^[_a-z][_a-z0-9]{3,}$/i;
if (pattern.???(????('username').value))
...
}
<form id='test_form' action='controller.php'>
Username: <input id='username' type='text' name='username'><br>
Password: <input id='password' type='???' name='password' pattern='^[_a-z][_a-z0-9]{3,}$'><br>
<input id='test_form_button' type='???' value='Submit'> // Just button, not submit
// If submit button is used, the click event may not be captured.
</form>